package com.medical.blood.controller;

import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.medical.blood.service.UserLoginService;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UserLoginController {

    @Autowired
    private UserLoginService userLoginService;

    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody Map<String, String> requestMap) {
        Map<String, Object> responseMap = new HashMap<>();
        try {
            String code = requestMap.get("code");
            String token = userLoginService.login(code);
            responseMap.put("token", token);
            responseMap.put("message", "登录成功");
            responseMap.put("success", true);
        } catch (WxErrorException e) {
            responseMap.put("message", "微信授权失败：" + e.getMessage());
            responseMap.put("success", false);
        }
        return responseMap;
    }
}